package com.xxtx.trade.agent.vo;

import cn.hutool.core.util.RandomUtil;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;

/**
 * 微信统一下单对象
 * @Author Jay.Wang
 */
public class WeiChatUnifyOrderEntity {
    /**
     * 设备号
     * 默认 WEB
     */
    private String device_info;
    /**
     * 商品描述
     * 必填
     */
    private String body;
    /**
     * 商品详情
     */
    private String detail;
    /**
     * 附加数据
     */
    private String attach;
    /**
     * 商户订单号
     * 必填
     */
    private String out_trade_no;
    /**
     * 货币类型
     * 默认 CNY
     */
    private String fee_type;
    /**
     * 订单总额 单位:分
     * 必填
     */
    private int total_fee;
    /**
     * 终端ip 用户实际ip
     * 必填
     */
    private String spbill_create_ip;
    /**
     * 交易起始时间 yyyyMMddHHmmss
     */
    private String time_start;
    /**
     * 交易失效时间 yyyyMMddHHmmss
     */
    private String time_expire;
    /**
     * 订单优惠标记
     */
    private String goods_tag;
    /**
     * 异步通知地址 不能携带参数
     * 必填
     */
    private String notify_url;
    /**
     * 交易类型
     * 必填
     */
    private String trade_type;
    /**
     * 指定支付方式
     */
    private String limit_pay;
    /**
     * 场景信息
     */
    private String scene_info;
    /**
     * 用户openId
     */
    private String openid;

    private static final long serialVersionUID = -3072800303815592761L;

    private String appid;

    private String mch_id;

    private String nonce_str;

    private String sign;

    private String sign_type;

    {
        this.nonce_str = RandomUtil.randomString(10);
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public void setMch_id(String mch_id) {
        this.mch_id = mch_id;
    }

    public void setNonce_str(String nonce_str) {
        this.nonce_str = nonce_str;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public void setSign_type(String sign_type) {
        this.sign_type = sign_type;
    }

    public String getAppid() {
        return appid;
    }

    public String getMch_id() {
        return mch_id;
    }

    public String getNonce_str() {
        return nonce_str;
    }

    public String getSign() {
        return sign;
    }

    public String getDevice_info() {
        return device_info;
    }

    public void setDevice_info(String device_info) {
        this.device_info = device_info;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

    public String getAttach() {
        return attach;
    }

    public void setAttach(String attach) {
        this.attach = attach;
    }

    public String getOut_trade_no() {
        return out_trade_no;
    }

    public void setOut_trade_no(String out_trade_no) {
        this.out_trade_no = out_trade_no;
    }

    public String getFee_type() {
        return fee_type;
    }

    public void setFee_type(String fee_type) {
        this.fee_type = fee_type;
    }

    public int getTotal_fee() {
        return total_fee;
    }

    public void setTotal_fee(int total_fee) {
        this.total_fee = total_fee;
    }

    public String getSpbill_create_ip() {
        return spbill_create_ip;
    }

    public void setSpbill_create_ip(String spbill_create_ip) {
        this.spbill_create_ip = spbill_create_ip;
    }

    public String getTime_start() {
        return time_start;
    }

    public void setTime_start(String time_start) {
        this.time_start = time_start;
    }

    public String getTime_expire() {
        return time_expire;
    }

    public void setTime_expire(String time_expire) {
        this.time_expire = time_expire;
    }

    public String getGoods_tag() {
        return goods_tag;
    }

    public void setGoods_tag(String goods_tag) {
        this.goods_tag = goods_tag;
    }

    public String getNotify_url() {
        return notify_url;
    }

    public void setNotify_url(String notify_url) {
        this.notify_url = notify_url;
    }

    public String getTrade_type() {
        return trade_type;
    }

    public void setTrade_type(String trade_type) {
        this.trade_type = trade_type;
    }

    public String getLimit_pay() {
        return limit_pay;
    }

    public void setLimit_pay(String limit_pay) {
        this.limit_pay = limit_pay;
    }

    public String getScene_info() {
        return scene_info;
    }

    public void setScene_info(String scene_info) {
        this.scene_info = scene_info;
    }

    public String getOpenid() {
        return openid;
    }

    public void setOpenid(String openid) {
        this.openid = openid;
    }

    /**
     * 转xml
     *
     * @return
     */
    public String toXml() {
        try {
            QName qName = new QName("xml");
            JAXBContext context = JAXBContext.newInstance(this.getClass());
            JAXBElement jaxbElement = new JAXBElement(qName, this.getClass(), this);
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            marshaller.marshal(jaxbElement, baos);
            return new String(baos.toByteArray(), "utf-8");
        } catch (JAXBException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
